﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

//Importamos
using EventSys.BussinesEntities;
using EventSys.BussinesLogic;
using Microsoft.Practices.Unity;

namespace EventSys.MVC4Web.Controllers
{
    
    public class DirectorController : Controller
    {
        [Dependency]
        public IDirectoresService directorService { get; set; }

        [Dependency]
        public ICarreraService carreraService { get; set; }

        //
        // GET: /Director/

        public ActionResult Index()
        {
            //var listaCarrera = carreraService.GetFromAllCarrera();
            var listaDirectores = directorService.GetFromAllDirectorByCriterio("");
            return View(listaDirectores);
        }

        [HttpPost]
        public ActionResult Index(string criterio)
        {
            var buscarDirector = directorService.GetFromAllDirectorByCriterio(criterio);

            return View(buscarDirector);
        }

        //  // GET: /Director/Create
        public ActionResult Create()
        {
            var carrera = carreraService.GetFromAllCarrera();
            ViewData["idCarrera"] = new SelectList(carrera, "idCarrera", "Carrera1");
            return View();
        }

        //  // POST: 
        [HttpPost]
        public ActionResult Create(Director collection)
        {
            //if (!TryUpdateModel(collection))
            //{
            //    try { }
            //    catch (Exception ex)
            //    {
            //        //ViewBag.updatError = "La creacion a fallado";
            //        ViewData["message"] = ex.Message;
            //        return View(collection);
            //    }
            //}

            directorService.RegistrarDirector(collection);
            return RedirectToAction("Index");
        }

        // GET: /Director/Edit/5
        public ActionResult Edit(int id)
        {
            var director = directorService.DirectorById(id);
            var carrera = carreraService.GetFromAllCarrera();
            ViewData["idCarrera"] = new SelectList(carrera, "idCarrera", "Carrera1");

            return View(director);
        }

        // POST: /Director/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, Director director)
        {
            try
            {
                directorService.EditarDirector(director);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ViewData["message"] = ex.Message;
                return View(director);
            }
        }
        /// ///////para validar el DNI/////
        public ActionResult validarDni(string dni)
        {
            var list = directorService.GetFromAllDirector().ToList();

            var rpt = new { existe = false, };

            try
            {
                var query = from d in list select d.DNI;

                foreach (var item in query)
                {
                    if (item == dni)
                    {
                        rpt = new
                        {
                            existe = true,
                        };
                    }
                }
                return Json(rpt, JsonRequestBehavior.AllowGet);//nos permite enviar el mensaje
            }
            catch (SystemException) { }
            return Json(rpt, JsonRequestBehavior.AllowGet);
        } 
            
        

        public ActionResult refreshCarrera()
        {
            var carrera = carreraService.GetFromAllCarrera();
            List<SelectListItem> list = new List<SelectListItem>();
            foreach (Carrera c in carrera)
            {
                list.Add(new SelectListItem() { Value = c.idCarrera.ToString(), Text = c.Carrera1 });
            }
            return Json(list, JsonRequestBehavior.AllowGet);
        }

    }
}
